#include "xTreeView.h"
#include "xTreeModel.h"
#include <QStandardItem>
#include "treeitem.h"
#include "qcomm.h"

xTreeView::xTreeView(QWidget *parent) : QTreeView(parent)
{
    // create tree m_pTreeModel
    m_pTreeModel = new xTreeModel(parent);
    setModel(m_pTreeModel);

    //initTree();

    //关联项目属性改变的信号和槽
    connect(m_pTreeModel, &QStandardItemModel::itemChanged, this, &xTreeView::slotTreeNodeSelected);
    connect(m_pTreeModel, SIGNAL(sigExpandItem(QModelIndex)), this,  SLOT(expand(QModelIndex)), Qt::UniqueConnection);
}

xTreeView::~xTreeView()
{

}

void xTreeView::slotAddFileToTree(const QString fileName)
{
    QStringList directories = fileName.split("/");
    QString directory;
    int size = directories.size();

    for(int i = 0; i < directories.size() - 1; i++)
    {
        directory.append(directories[i]);
        directory.append("/");
    }

    QString title = directories[size - 1];
    QStringList file;
    file.push_back(directory);
    file.push_back(title);
    m_pTreeModel->addSceneData(file);

}

void xTreeView::slotRemoveAllFile()
{
    m_pTreeModel->removeAllData();
}


void xTreeView::slotTreeNodeSelected(QStandardItem* item)
{
    if(item == nullptr)
        return;

    sigTreeNodeSelected(item);
}



